package cn.lili.modules.grab.dingdong.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random;
import java.util.UUID;

/**
 * @description: AES密钥
 * @author: zlb
 * @create: 2021-04-26 11:09
 **/
public class AESUtil {
    // 密钥算法
    private static final String KEY_ALGORITHM = "AES";

    //AES IV
    public static String ivString = "106d46f29f9ea389";

    //模式和填充方式
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

    // 默认key，测试用的
    private static final String KEY = "1616484102912730";

    /**
     * 生成密钥
     */
    public static String initkey() throws Exception {
        String key = getUUId();
        System.out.println("明文lock===" + key);
        return Base64.getEncoder().encodeToString(key.getBytes());
    }

    public static String getUUId() {
        int first = new Random(10).nextInt(8) + 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return first + String.format("%015d", hashCodeV);
    }

    /**
     * 转换密钥
     */
    public static SecretKeySpec toKey(byte[] key) throws Exception {
        return new SecretKeySpec(key, KEY_ALGORITHM);
    }

    /**
     * 加密数据
     *
     * @param data 待加密数据
     * @param key  密钥
     * @return 加密后的数据
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] iv = ivString.getBytes();//IV向量固定
        // 还原密钥
        SecretKeySpec k = toKey(Base64.getDecoder().decode(key));

        // 实例化Cipher对象，它用于完成实际的加密操作
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        // 初始化Cipher对象，设置为加密模式
        cipher.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv));

        // 执行加密操作。加密后的结果通常都会用Base64编码进行传输
        return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
    }

    /**
     * 解密数据
     *
     * @param data 待解密数据
     * @param key  密钥
     * @return 解密后的数据
     */
    public static String decrypt(String data, String key) throws Exception {
        byte[] iv = ivString.getBytes();//IV向量固定
        SecretKeySpec k = toKey(Base64.getDecoder().decode(key));
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        // 初始化Cipher对象，设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
        // 执行解密操作
        return new String(cipher.doFinal(Base64.getDecoder().decode(data)));
    }

    /**
     * 加密数据
     * 用固定不变的密钥k
     *
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static String encrypt(String data) throws Exception {
        return encrypt(data, KEY);
    }

    /**
     * 解密数据
     * 用固定不变的密钥k
     *
     * @param data 待解密数据
     * @return 解密后的数据
     */
    public static String decrypt(String data) throws Exception {
        return decrypt(data, KEY);
    }

    public static void main(String[] args) throws Exception {
/*        String key = initkey();
        System.out.println("key: " + key);
        JSONObject obj = new JSONObject();
        obj.put("app_key","58a5c84f-39a5-9f26-cb30-14a403a11a06");
        obj.put("time",Long.valueOf(String.valueOf("1614666916805")));

        JSONObject content = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("goods_id","0");
        data.put("cate_id","0");
        data.put("goods_name","");
        data.put("sort","time");
        data.put("sort_type","desc");
        data.put("page_size","15");
        data.put("page",1);
        data.put("device","6");

        JSONObject base = new JSONObject();
        base.put("equipment","");
        base.put("time",Long.valueOf("1614666916805"));
        base.put("source","99");
        base.put("version","v3.1.2");
        base.put("token","ERGK6BEE347A4F49F545GH2BEB5206C7");

        content.put("data",data);
        content.put("base",base);

        String encrypt = encrypt(content.toJSONString(),key);

        System.out.println(encrypt);
        System.out.println(decrypt(encrypt,key));

        String content1 = "dkVYUCUsXRVtKwaDjR5l6HfxJuLJIaITyH1Rp7JULX93ONQlqjO39KHTClKbW/LrYs29Fv9hLf+6j36udIlsGzI1ju5v6i8LOVd3eauByv3t6uRcKRBLm3xG0+UCFVctE+m9vWEvJquUV31KxmkeKbRP6LUpfirdzMyU+/MhBhEOnLLVY3xOYkDQMUGdhq6lRac3ZgjeYw4l87AW6J4BiNy4DVAK2PrA1d1VtBlqUsD0qa19haFv+fu8sl3SzVlBlbkoLZoV3TnFXQ4H4hx3t7kQS9I2PE9CzpFd+xjj9OLqhEZySghmn3nflbRwFQJZ6QHvH0SW4C90VjJSzaUCEo6bgxg84nsMrbt42A04ITSE7YauCEKMoCEIyZmxj0aAI3kMcKjB3qgUpP0HmBZsarHQJUQYJknLImp96GvG4HTLy+uo3mqwLkX3se21zjqG";

        //回调的lock内容
        String lock = "QduNExzjcTFOnKDwIJow4mEbTsg6AUUV7XjOmdETe8Fh1Nu1pY+RfHtNVaPzzHSPHs/RagAq+xzrBwbm9EQhb5YCmj36/OklvR0dyMYMo4X0LcCcHO5DEFGQpc+TyrQriOdqOkf6PGyBrhCmjawkXmMtvhdU2gUVoW8OlSLo5Us=";
        //公钥
        String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbXV+2TIBIaRDopdtdWoU9MD7rTqZ8toEw9dYWgNQ+fd22IKbXbaQpQVMQwdQ56DxtSyOvlBmBupXYPc+48fl0/1SGEYf9RShgUE4NrsYvsqEAd3Y1r/HoIltHwjgimsGn8zcXuH6daUl+WcwPjGRPZ+oUPcd8zXnCRH+Gf26RrwIDAQAB";

        try {
           String lockj =  RSAUtil.pubDecrypt(lock,publicKey);
           String data1 = decrypt(content1,lockj);
           System.out.println(data1);
        } catch (Exception e) {
            e.printStackTrace();
        }*/


    }
}